define(["jquery", "easy-admin"], function ($, ea) {

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'mall.upload_goods/staffIndex',
        add_url: 'mall.upload_goods/add',
        edit_url: 'mall.upload_goods/edit',
        detail_url: 'mall.upload_goods/detail',
    };

    var Controller = {

        staffIndex: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="true"',
                    }]
                ],
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID', search: false},
                    {field: 'sort', width: 80, title: '排序', edit: 'text', search: false},
                    {field: 'is_top', width: 80, title: '置顶', selectList: {0: '否', 1: '是'}},
                    {field: 'title', minWidth: 80, title: '商品名称'},
                    {field: 'logo', minWidth: 80, title: '商品图片', search: false, templet: ea.table.image},
                    {field: 'collage_num', minWidth: 80, title: '拼团人数', search: false},
                    {field: 'market_price', width: 100, title: '市场价', templet: ea.table.price, search: false},
                    {field: 'sales', width: 80, title: '销量', search: false},
                    {field: 'is_novices_exclusive', title: '新手专属商品', width: 85, selectList: {0: '否', 1: '是'}},
                    {
                        field: 'status',
                        title: '状态',
                        width: 85,
                        selectList: {0: '下架', 1: '上架'},
                        tips: '上架|下架'
                    },
                    {field: 'create_time', minWidth: 80, title: '创建时间', search: 'false'},
                    {
                        field: 'create_time_left',
                        minWidth: 80,
                        title: '创建开始时间',
                        search: 'rangeLeft',
                        hide: true,
                        searchOp: 'rangeLeft'
                    },
                    {
                        field: 'create_time_right',
                        minWidth: 80,
                        title: '创建结束时间',
                        search: 'rangeRight',
                        hide: true,
                        searchOp: 'rangeRight'
                    },
                    {
                        width: 250,
                        title: '操作',
                        templet: function (row) {
                            var operat = []
                            if (row.status === 0) {
                                operat.push(
                                    [{
                                        text: '编辑',
                                        url: init.edit_url,
                                        method: 'open',
                                        field: 'id',
                                        auth: 'edit',
                                        class: 'layui-btn layui-btn-xs layui-btn-success',
                                        extend: 'data-full="true"',
                                    }],
                                )
                            } else {
                                operat.push(
                                    [{
                                        text: '查看',
                                        url: init.detail_url,
                                        method: 'open',
                                        field: 'id',
                                        auth: 'detail',
                                        class: 'layui-btn layui-btn-xs layui-btn-success',
                                        extend: 'data-full="true"',
                                    }],
                                )
                            }

                            return ea.table.tool(row, {
                                init, // 初始化的数据变量
                                operat: operat
                            }); // 其他状态下 没有操作按钮返回空字符串即可
                        }
                    }
                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        detail: function () {
            ea.listen();
        },
    };
    $("body").on('click', ".deleteBtn", function () {
        //元素移除前校验是否被引用
        var approvalName = $(this).parent().prev('div.layui-input-inline').children().val();
        var parentEle = $(this).parent().parent();
        $(this).parent().remove();
    });
    var form = layui.form
    form.on('select(novice)', function (data) {
        if (data.value == 0) {
            $('#novice_price').hide()
        } else {
            $('#novice_price').show()
        }

    });
    return Controller;

});

window.insertInput = function () {
    var str = '            <div class="layui-inline" id="bc">\n' +
        '                <label class="layui-form-label"></label>\n' +
        '                <div class="layui-input-inline">\n' +
        '                    <input type="text" name="spec[]" class="layui-input" lay-verify="required" placeholder="请输入规格"\n' +
        '                           autocomplete="off" value="">\n' +
        '                </div>\n' +
        '                <button class="layui-btn layui-btn-danger deleteBtn" id="" ><i class="layui-icon "></i>删除规格</button>\n' +
        '            </div>'


    $('#bc').append(str)
    // $('#bc').clone().insertBefore('#insertBtn');//复制后追加
};

